package common.core.domain.http;

import io.netty.handler.codec.http.HttpMethod;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * 读取routeMap.xml
 */
@Slf4j
public class HttpApiRoute {
    private static final String routeName = "/routeMap.xml";
    private static final String apiNode   = "httpApi";
    private static final String apiName   = "name";
    private static final String apiProtocol = "protocol";
    private static final String apiMethod = "method";
    private static final String apiHandler = "handler";
    private static final String apiVersion = "version";


    public static final Map<String, HttpApi> apiMap = new HashMap<String, HttpApi>();

    static {
        init();
    }

    public static void init(){
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(HttpApiRoute.class.getResourceAsStream(routeName));
            NodeList apiList = doc.getElementsByTagName(apiNode);
            for (int i = 0, apiLength = apiList.getLength(); i < apiLength; i++) {
                Element element = (Element) apiList.item(i);

                HttpApi api = new HttpApi();
                for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
                    if (node.getNodeType() == Node.ELEMENT_NODE) {

                        String name = node.getNodeName();
                        String value = node.getFirstChild().getNodeValue();
                        String type = HttpMethod.GET.name();

                        switch (name) {
                            case apiName:
                                api.setName(value);
                                break;
                            case apiProtocol:
                                api.setProtocol(value);
                                break;
                            case apiMethod:
                                if(node.hasAttributes()){
                                    type = node.getAttributes().getNamedItem("type").getNodeValue();
                                }
                                api.getMethod().put(value,type);
                                break;
                            case apiHandler:
                                api.setHandler(value);
                                break;
                            case apiVersion:
                                api.setVersion(value);
                                break;
                            default:
                                break;
                        }
                    }
                }
                apiMap.put(api.getName(),api);
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
